package forms;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;

import model.ReviewerModel;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import db.DbException;

public class ReviewerFormInsert extends ActionForm {

    private static final long serialVersionUID = 1L;

    private String username = null;
    private String password = null;

    
    public String getUsername() {
        return username;
    }

    
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Reset all properties to their default values.
     * 
     * @param mapping
     *            The mapping used to select this instance
     * @param request
     *            The servlet request we are processing
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        setUsername(null);
        setPassword(null);
    }

    /**
     * Validate the input data. If validation fails, then report the errors in
     * students.jsp, and specifically where <html:errors/> is located.
     * 
     * @param mapping
     *            The mapping used to select this instance
     * @param request
     *            The servlet request we are processing
     */
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        // whether or not the username is unique
        String unique = null;
        RowSet rset;
        // if there is a lower case
        boolean lower = false;
        // if there is an upper case
        boolean upper = false;

        // check if username is unique
        try {
            rset = ReviewerModel.fetchReviewer(username);
            if(rset.next()){
                unique = rset.getString("username");
            }
        } catch (DbException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // LOTS OF UNCERTAINTY HERE
        if(unique != null && unique.equals(username)){ // is this how you check for unique ness
            errors.add("userMsgTag1", new ActionMessage("error.username.taken")); // is this how you write an error message
        }
        
        // check if username is long enough
        if ((username == null) || (username.length() < 3))
            errors.add("userMsgTag2", new ActionMessage("errors.minlength",
                    "User Name", 3));

        // check if password is long enough
        if ((password == null) || (password.length() < 8))
            errors.add("passwordMsgTag1", new ActionMessage("errors.minlength",
                    "Password", 8));

        for(int i = 0; i < password.length(); i++){
            // checks for upper case
            if(upper == false){
                for(char c='A'; c <='Z'; c++){
                    if(password.charAt(i)==c){
                        upper = true;
                        break;
                    }
                }
            }
            // checks for lower case
            if(lower == false){
                for(char c='a'; c <='z'; c++){
                    if(password.charAt(i)==c){
                        lower = true;
                        break;
                    }
                }
            }
            // if there is both upper and lower
            if(upper && lower){
                break;
            }
        }
        
        // check if password contains both upper and lower case
        if(upper == false || lower == false){
            errors.add("passwordMsgTag2", new ActionMessage("error.password.cases"));
        }
        
        return errors;
    }

}